
exports.create = function (sock) {
    var sockwrap = new SockWrap(sock);
    return sockwrap;
}

exports.clearEvents = function (evts) {
    evts.forEach(function (rm) {
        rm();
    });
}

function SockWrap(sock) {
    this._sock = sock;
    this._on = [];

    this.on = function (msg, fn) {
        var wrap = this;
        var callwrap = function (data) {       
            wrap.clearon();
            fn(data);
        }
        this._sock.on(msg, callwrap);

        var rm = function () {
            wrap._sock.removeListener(msg, callwrap);
        }

        this._on.push(rm);
        return rm;
    };

    this.bind = function (msg, fn) {
        this._sock.on(msg, fn);
    };

    this.clearon = function () {
        this._on.forEach(function (rm) { rm(); });
        this._on = [];
    }
}